package com.wcq.studentbackend.controller;

import com.wcq.studentbackend.dto.ClassDTO;
import com.wcq.studentbackend.dto.ClassRequestDTO;
import com.wcq.studentbackend.service.ClassService;
// import org.springframework.http.HttpStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 班级管理 前端控制器
 * </p>
 *
 * @author Gemini
 * @since ${.now?date}
 */
@RestController
@RequestMapping("/api/classes") // 统一的API前缀
@Api(value = "班级管理")
@Slf4j
public class ClassController {

    private final ClassService classService;

    // Spring 推荐使用构造器注入
    public ClassController(ClassService classService) {
        this.classService = classService;
    }

    /**
     * 获取所有班级列表
     * GET /api/classes
     */
    /*@GetMapping
    public ResponseEntity<List<ClassDTO>> getAllClasses() {
        List<ClassDTO> classes = classService.getAllClasses();
        return ResponseEntity.ok(classes);
    }*/

    /**
     * 根据ID获取班级信息
     * GET /api/classes/{id}
     */
    @ApiOperation(value = "获取班级信息接口")
    @GetMapping("/{id}")
    public ResponseEntity<ClassDTO> getClassById(@PathVariable Integer id) {
        log.info("获取班级信息，id={}", id);
        ClassDTO classDTO = classService.getClassById(id);
        if (classDTO != null) {
            return ResponseEntity.ok(classDTO);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 新增班级
     * POST /api/classes
     * RequestBody: ClassRequestDTO
     */
    @PostMapping
    @ApiOperation(value = "创建班级接口")
    public ResponseEntity<ClassDTO> createClass(/*@Valid*/ @RequestBody ClassRequestDTO classRequestDTO) {
        log.info("创建班级，参数={}", classRequestDTO);
        ClassDTO createdClass = classService.createClass(classRequestDTO);
        // 通常创建成功返回 201 Created，并带上新资源的URI和内容
        // return ResponseEntity.created(URI.create("/api/classes/" + createdClass.getId())).body(createdClass);
        return ResponseEntity.status(201).body(createdClass); // 简化版
    }

    /**
     * 更新班级信息
     * PUT /api/classes/{id}
     * RequestBody: ClassRequestDTO
     */
    @ApiOperation(value = "更新班级接口")
    @PutMapping("/{id}")
    public ResponseEntity<ClassDTO> updateClass(@PathVariable Integer id, /*@Valid*/ @RequestBody ClassRequestDTO classRequestDTO) {
        log.info("更新班级信息，id={}, classRequestDTO={}", id, classRequestDTO);
        ClassDTO updatedClass = classService.updateClass(id, classRequestDTO);
        if (updatedClass != null) {
            return ResponseEntity.ok(updatedClass);
        }
        return ResponseEntity.notFound().build(); // 或者根据业务逻辑返回其他错误码
    }

    /**
     * 删除班级
     * DELETE /api/classes/{id}
     */
    @ApiOperation(value = "删除班级接口")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteClass(@PathVariable Integer id) {
        log.info("删除班级，id={}", id);
        boolean deleted = classService.deleteClass(id);
        if (deleted) {
            return ResponseEntity.noContent().build(); // 204 No Content
        }
        // 如果删除失败（例如，因为班级不存在，或者有其他业务约束），可以返回 404 Not Found 或其他适当的错误码
        return ResponseEntity.notFound().build(); 
    }
    // ...
    @GetMapping
    @ApiOperation(value = "获取所有班级接口")
    public ResponseEntity<List<ClassDTO>> getAllClasses() {
        log.info("获取所有班级");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            System.out.println("ClassController - getAllClasses - Current Authentication: " + authentication);
            System.out.println("ClassController - getAllClasses - Is Authenticated: " + authentication.isAuthenticated());
            System.out.println("ClassController - getAllClasses - Principal: " + authentication.getPrincipal());
            System.out.println("ClassController - getAllClasses - Authorities: " + authentication.getAuthorities());
        } else {
            System.out.println("ClassController - getAllClasses - Authentication is NULL");
        }
        List<ClassDTO> classes = classService.getAllClasses();
        return ResponseEntity.ok(classes);
    }
} 